package myservice.android.activities;

import myservice.android.Global;
import myservice.android.R;
import myservice.android.utilities.CheckBoxMonitor;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class CustomizeGraphsActivity extends BaseSaveableActivity {	
	
	private TextView fromDate, toDate;
	private CheckBox totalTimeCheckbox, ministryTimeCheckbox, creditTimeCheckbox, magazinesCheckbox, booksCheckbox,
	                 brochuresCheckbox, tractsCheckbox, videosCheckbox, returnCallsCheckbox, bibleStudentsCheckbox;
	private Button fromPreviousButton, fromNextButton, toPreviousButton, toNextButton, saveButton;
	
	@Override
	protected void initializeLayoutIdAndTitle() {
		layoutId = R.layout.customize_graphs;
		activityTitle = Global.res.getString(R.string.title_customize_graphs);
	}

	@Override
	protected void retrieveViews() {
        fromDate = (TextView) findViewById(R.id.fromMonth);
        toDate = (TextView) findViewById(R.id.toMonth);
        totalTimeCheckbox = (CheckBox) findViewById(R.id.showTotalTimeCheckbox);        
        ministryTimeCheckbox = (CheckBox) findViewById(R.id.showMinistryTimeCheckbox);        
        creditTimeCheckbox = (CheckBox) findViewById(R.id.showCreditTimeCheckbox);        
        magazinesCheckbox = (CheckBox) findViewById(R.id.showMagazinesCheckbox);        
        booksCheckbox = (CheckBox) findViewById(R.id.showBooksCheckbox);        
        brochuresCheckbox = (CheckBox) findViewById(R.id.showBrochuresCheckbox);  
        tractsCheckbox = (CheckBox) findViewById(R.id.showTractsCheckbox);
        videosCheckbox = (CheckBox) findViewById(R.id.showVideosCheckbox);        
        returnCallsCheckbox = (CheckBox) findViewById(R.id.showReturnCallsCheckbox);        
        bibleStudentsCheckbox = (CheckBox) findViewById(R.id.showBibleStudentsCheckbox); 
        fromPreviousButton = (Button) findViewById(R.id.fromPreviousButton);
        fromNextButton = (Button) findViewById(R.id.fromNextButton);
        toPreviousButton = (Button) findViewById(R.id.toPreviousButton);
        toNextButton = (Button) findViewById(R.id.toNextButton);
        saveButton = (Button) findViewById(R.id.saveButton);
	}

	@Override
	protected void loadActivityParameters() { }

	@Override
	protected void assignListeners() {
		
		totalTimeCheckbox.setOnCheckedChangeListener(new CheckBoxMonitor(this));
		ministryTimeCheckbox.setOnCheckedChangeListener(new CheckBoxMonitor(this));
		creditTimeCheckbox.setOnCheckedChangeListener(new CheckBoxMonitor(this));
		magazinesCheckbox.setOnCheckedChangeListener(new CheckBoxMonitor(this));
		booksCheckbox.setOnCheckedChangeListener(new CheckBoxMonitor(this));
		brochuresCheckbox.setOnCheckedChangeListener(new CheckBoxMonitor(this));
		tractsCheckbox.setOnCheckedChangeListener(new CheckBoxMonitor(this));
		videosCheckbox.setOnCheckedChangeListener(new CheckBoxMonitor(this));
		returnCallsCheckbox.setOnCheckedChangeListener(new CheckBoxMonitor(this));
		bibleStudentsCheckbox.setOnCheckedChangeListener(new CheckBoxMonitor(this));
		
		fromPreviousButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				switchFromMonth(-1);				
			}			
		});
		
		fromNextButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				switchFromMonth(1);				
			}			
		});
		
		toPreviousButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				switchToMonth(-1);				
			}			
		});
		
		toNextButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				switchToMonth(1);				
			}			
		});
		
        saveButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				save();
				finish();
			}
        }); 
		
	}		         
    
    @Override
    protected void updateUi() {
    	
    	fromDate.setText(Global.monthNames[Global.fromMonth]+" "+Global.fromYear);
    	toDate.setText(Global.monthNames[Global.toMonth]+" "+Global.toYear);
    	
    	totalTimeCheckbox.setChecked(Global.showTotalTimeGraph);
    	ministryTimeCheckbox.setChecked(Global.showMinistryTimeGraph);
    	creditTimeCheckbox.setChecked(Global.showCreditTimeGraph);
    	magazinesCheckbox.setChecked(Global.showMagazinesGraph);
    	booksCheckbox.setChecked(Global.showBooksGraph);
    	brochuresCheckbox.setChecked(Global.showBrochuresGraph);
    	tractsCheckbox.setChecked(Global.showTractsGraph);
    	videosCheckbox.setChecked(Global.showVideosGraph);
    	returnCallsCheckbox.setChecked(Global.showReturnCallsGraph);
    	bibleStudentsCheckbox.setChecked(Global.showBibleStudentsGraph);
    	
    }
    
    @Override
    protected void save() {
    	Global.showTotalTimeGraph=totalTimeCheckbox.isChecked();
    	Global.showMinistryTimeGraph=ministryTimeCheckbox.isChecked();
    	Global.showCreditTimeGraph=creditTimeCheckbox.isChecked();
    	Global.showMagazinesGraph=magazinesCheckbox.isChecked();
    	Global.showBooksGraph=booksCheckbox.isChecked();
    	Global.showBrochuresGraph=brochuresCheckbox.isChecked();
    	Global.showTractsGraph=tractsCheckbox.isChecked();
    	Global.showVideosGraph=videosCheckbox.isChecked();
    	Global.showReturnCallsGraph=returnCallsCheckbox.isChecked();
    	Global.showBibleStudentsGraph=bibleStudentsCheckbox.isChecked();
    	
    }
    
	private void switchFromMonth(int direction) {
		
		if (direction==1 && Global.fromYear*12+Global.fromMonth+1>=Global.toYear*12+Global.toMonth)
			return;
		
		Global.fromMonth = Global.fromMonth + direction;
		
		if (Global.fromMonth<0) {
			Global.fromMonth=11; Global.fromYear--;
		}
		
		if (Global.fromMonth>11) {
			Global.fromMonth=0; Global.fromYear++;
		}
		
    	fromDate.setText(Global.monthNames[Global.fromMonth]+" "+Global.fromYear);
    	
    	activityContentModified = true;
		
	}   
	
	private void switchToMonth(int direction) {
		
		if (direction==-1 && Global.toYear*12+Global.toMonth-1<=Global.fromYear*12+Global.fromMonth)
			return;
		
		
		Global.toMonth = Global.toMonth + direction;
		
		if (Global.toMonth<0) {
			Global.toMonth=11; Global.toYear--;
		}
		
		if (Global.toMonth>11) {
			Global.toMonth=0; Global.toYear++;
		}
		
    	toDate.setText(Global.monthNames[Global.toMonth]+" "+Global.toYear);
    	
    	activityContentModified = true;
		
	}
    
}